package com.xls.boxlibrary.tixianjilu.jifenduihuanjilu;


import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.xls.boxlibrary.Consts.IntentConsts;
import com.xls.boxlibrary.Consts.NetConsts;
import com.xls.boxlibrary.R;
import com.xls.boxlibrary.adapter.JiFenDuiHuanJiLuAdapter;
import com.xls.boxlibrary.adapter.TiXianJiLuAdapter;
import com.xls.boxlibrary.duihuanjibi.JinBiNetWorkUtil;
import com.xls.boxlibrary.entity.JiFenDuiHuanJiLuEntity;
import com.xls.boxlibrary.entity.PageEntity;
import com.xls.boxlibrary.entity.TiXianJiLuEntity;
import com.xls.boxlibrary.network.RequestCallBack;
import com.xls.boxlibrary.xrecyclerview.XRecyclerView;

import java.util.ArrayList;

/**
 * MVPPlugin
 */

public class JiFenDuiHuanJiLuFragment  extends Fragment {
    public static JiFenDuiHuanJiLuFragment newInstance(String appid) {
        Bundle args = new Bundle();
        args.putString(IntentConsts.id,appid);
        JiFenDuiHuanJiLuFragment fragment = new JiFenDuiHuanJiLuFragment();
        fragment.setArguments(args);
        return fragment;
    }
    private XRecyclerView jilu_list;

    String appid="";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_jifenduihuanjilu, container, false);
        appid=getArguments().getString(IntentConsts.id);
        return view;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        jilu_list = view.findViewById(R.id.jilu_list);
        jilu_list.setLayoutManager(new LinearLayoutManager(getContext()));
        jilu_list.setAdapter(getTiXianJiLuAdapter());
        jilu_list.setLoadingListener(getLoadingListener());
        jilu_list.refresh();
    }


    JiFenDuiHuanJiLuAdapter tiXianJiLuAdapter;
    ArrayList<JiFenDuiHuanJiLuEntity> tiXianJiLuDataList=new ArrayList<>();
    int pageSize= NetConsts.PageSize,pageNum=NetConsts.PageNum;
    public RecyclerView.Adapter getTiXianJiLuAdapter() {
        if (tiXianJiLuAdapter==null){
            tiXianJiLuAdapter=new JiFenDuiHuanJiLuAdapter(getContext(),tiXianJiLuDataList);
        }
        return tiXianJiLuAdapter;
    }


    public XRecyclerView.LoadingListener getLoadingListener() {

        return new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                pageNum=NetConsts.PageNum;
                getSourceData();
            }

            @Override
            public void onLoadMore() {
                pageNum=pageNum+1;
                getSourceData();
            }
        };
    }

    public void getSourceData() {
        JinBiNetWorkUtil.getInstance().getMyScoreExchangeRecords(getContext(),pageSize, pageNum,appid, new RequestCallBack<PageEntity<JiFenDuiHuanJiLuEntity>>() {
            @Override
            public void onStart() {

            }

            @Override
            public void onStop() {
                if (jilu_list==null||getContext()==null)return;
                jilu_list.refreshComplete();
                jilu_list.loadMoreComplete();
            }

            @Override
            public void onRequestSuccess(PageEntity<JiFenDuiHuanJiLuEntity> requestData) {
                if (jilu_list==null||getContext()==null||requestData==null)return;
                if (pageNum==NetConsts.PageNum){
                    tiXianJiLuDataList.clear();
                }
                if (requestData.getRecords()!=null){
                    tiXianJiLuDataList.addAll(requestData.getRecords());
                }
                if (tiXianJiLuDataList.size()>=requestData.getTotal()){
                    jilu_list.setNoMore(true);
                }else {
                    jilu_list.setNoMore(false);
                }
                tiXianJiLuAdapter.notifyDataSetChanged();
            }

            @Override
            public void onRequestfail(String errno, String errmsg) {

            }
        });
    }

}
